//
//  CMDBSettingViewController.swift
//  CMDB
//
//  Created by 王磊 on 18/05/2017.
//  Copyright © 2017 CMDB. All rights reserved.
//

import UIKit

class CMDBSettingViewController: NormalTitleViewController {
    
    override func setup() {
        super.setup()
        
        buildSubviews()
        
    }
}
extension CMDBSettingViewController {
    
    private func buildSubviews() {
        
        contentView!.backgroundColor = UIColor.Hex(0xE2E2E2)
        
        var y = 20 * CMDB_Rate
        
        let height: CGFloat = 44
        
        let modifyPwd = CMDBSelectedView.getInstance()
        
        modifyPwd.addTarget(self, action: #selector(modifyPwdItem), forEvent: TSItemClickType.ButtonItem)
        
        modifyPwd.update("修改密码")
        
        contentView!.addSubview(modifyPwd)
        
        modifyPwd.frame = CGRectMake(0, y, CMDB_Screen_Width, height)
        
        y += height
        
        let modifyPhone = CMDBSelectedView.getInstance()
        
        modifyPhone.addTarget(self, action: #selector(modifyPhoneItem), forEvent: TSItemClickType.ButtonItem)
        
        modifyPhone.update("变更手机号")
        
        contentView!.addSubview(modifyPhone)
        
        modifyPhone.frame = CGRectMake(0, y, CMDB_Screen_Width, height)
        
        y += height
        
        let modifyEmail = CMDBSelectedView.getInstance()
        
        modifyEmail.addTarget(self, action: #selector(modifyEmailItem), forEvent: TSItemClickType.ButtonItem)
        
        modifyEmail.update("变更邮箱")
        
        contentView!.addSubview(modifyEmail)
        
        modifyEmail.frame = CGRectMake(0, y, CMDB_Screen_Width, height)
        
        y += height + 20 * CMDB_Rate
        
        let userManage = CMDBSelectedView.getInstance()
        
        userManage.addTarget(self, action: #selector(userManageItem), forEvent: TSItemClickType.ButtonItem)
        
        userManage.update("二级用户管理")
        
        contentView!.addSubview(userManage)
        
        userManage.frame = CGRectMake(0, y, CMDB_Screen_Width, height)
        
        y += height
        
        let functionAuth = CMDBSelectedView.getInstance()
        
        functionAuth.addTarget(self, action: #selector(functionAuthItem), forEvent: TSItemClickType.ButtonItem)
        
        functionAuth.update("功能授权")
        
        contentView!.addSubview(functionAuth)
        
        functionAuth.frame = CGRectMake(0, y, CMDB_Screen_Width, height)
        
        y += height
        
        let clearCache = CMDBSelectedView.getInstance()
        
        clearCache.addTarget(self, action: #selector(clearCacheItem), forEvent: TSItemClickType.ButtonItem)
        
        clearCache.update("清除缓存")
        
        contentView!.addSubview(clearCache)
        
        clearCache.frame = CGRectMake(0, y, CMDB_Screen_Width, height)
        
        y += height + 20 * CMDB_Rate
        
        let privacy = CMDBSelectedView.getInstance()
        
        privacy.addTarget(self, action: #selector(privacyItem), forEvent: TSItemClickType.ButtonItem)
        
        privacy.update("隐私设置")
        
        contentView!.addSubview(privacy)
        
        privacy.frame = CGRectMake(0, y, CMDB_Screen_Width, height)
        
        y += height
        
        
        let about = CMDBSelectedView.getInstance()
        
        about.addTarget(self, action: #selector(aboutItem), forEvent: TSItemClickType.ButtonItem)
        
        about.update("关于我们")
        
        contentView!.addSubview(about)
        
        about.frame = CGRectMake(0, y, CMDB_Screen_Width, height)
        
        y += height + 20 * CMDB_Rate
        
        let logout = UIButton(type: .Custom)
        
        logout.setBackgroundImage(UIImage.yy_imageWithColor(UIColor.whiteColor()), forState: .Normal)
        logout.setBackgroundImage(UIImage.yy_imageWithColor(word12Color), forState: .Highlighted)
        contentView!.addSubview(logout)
        
        logout.titleLabel?.font = CMDB_15_FONT
        
        //        logout.titleLabel?.textColor = word54Color
        
        logout.setTitle("退出登录", forState: .Normal)
        
        logout.setTitle("退出登录", forState: .Highlighted)
        
        logout.setTitleColor(word54Color, forState: .Normal)
        
        logout.setTitleColor(word54Color, forState: .Highlighted)
        
        logout.addTarget(self, action: #selector(logoutItem), forControlEvents: .TouchUpInside)
        
        contentView!.addSubview(logout)
        
        logout.frame = CGRectMake(0, y, CMDB_Screen_Width, height)
    }
}
// MARK: item 点击处理
extension CMDBSettingViewController {
    
    @objc private func modifyPwdItem(argu: TSArguInfo) {
        
        let modifyPwd = CMDBModifyPwdViewController()
        
        modifyPwd.title = "修改密码"
        
        navigationController?.pushViewController(modifyPwd, animated: true)
        
    }
    
    @objc private func modifyPhoneItem() {
        
        let verifyPwd = CMDBVertifyPwdViewController()
        
        verifyPwd.title = "验证密码"
        
        verifyPwd.verifyType = .Phone
        
        navigationController?.pushViewController(verifyPwd, animated: true)
    }
    @objc private func modifyEmailItem() {
        let modifyEmail = CMDBModifyEmailViewController()
        
        modifyEmail.title = "变更邮箱"
        
        navigationController?.pushViewController(modifyEmail, animated: true)
        
    }
    
    @objc private func userManageItem() {
        
        let userManage = CMDBUserManageViewController()
        
        userManage.title = "二级用户管理"
        
        navigationController?.pushViewController(userManage, animated: true)
        
    }
    
    @objc private func functionAuthItem() {
        
        //        let modifyPwd = CMDBModifyPwdViewController()
        //
        //        modifyPwd.title = "修改密码"
        //
        //        navigationController?.pushViewController(modifyPwd, animated: true)
    }
    
    @objc private func clearCacheItem() {
        
        let actionSheet = ACActionSheet(title: "是否清楚缓存?", cancelButtonTitle: "取消", destructiveButtonTitle: "", otherButtonRedTitles: ["确定"]) { (idx) in
            
            if idx == 0 {
                
                printLog(0)
            }
        }
        actionSheet.show()
    }
    
    @objc private func privacyItem() {
        
        let privacy = CMDBPrivacyViewController()
        
        privacy.title = ""
        
        navigationController?.pushViewController(privacy, animated: true)
    }
    
    @objc private func aboutItem() {
        
        let about = CMDBAboutViewController()
        
        about.title = "关于我们"
        
        navigationController?.pushViewController(about, animated: true)
    }
    @objc private func logoutItem() {
        
        let actionSheet = ACActionSheet(title: "是否退出登录?", cancelButtonTitle: "取消", destructiveButtonTitle: "", otherButtonRedTitles: ["确定"]) { (idx) in
            
            if idx == 0 {
                
                
            }
            
        }
        actionSheet.show()
        
    }
}
